<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/blog/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="SpringBoot创建项目,">










<meta name="description" content="本文以商品管理功能开发为例，讲解使用SpringBoot开发通用功能的过程">
<meta name="keywords" content="SpringBoot创建项目">
<meta property="og:type" content="article">
<meta property="og:title" content="【四】SpringBoot创建项目——通用功能开发">
<meta property="og:url" content="https://aiolos123.gitee.io/blog/2020/01/15/springboot-step-by-step - 3/index.html">
<meta property="og:site_name" content="Aiolos">
<meta property="og:description" content="本文以商品管理功能开发为例，讲解使用SpringBoot开发通用功能的过程">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200114100606562.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200114104128581.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200114113759017.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200114164405962.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200114164405962.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200115055300547.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200115063316018.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200115065051054.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200115065429105.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200117152628727.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200117123751924.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200117151137494.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200115125621881.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200117161400094.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200118064344937.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200118064948102.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200118065541708.jpg">
<meta property="og:image" content="https://aiolos123.gitee.io/blog/images/20200118070922102.jpg">
<meta property="og:updated_time" content="2020-01-19T01:06:25.620Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【四】SpringBoot创建项目——通用功能开发">
<meta name="twitter:description" content="本文以商品管理功能开发为例，讲解使用SpringBoot开发通用功能的过程">
<meta name="twitter:image" content="https://aiolos123.gitee.io/blog/images/20200114100606562.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://aiolos123.gitee.io/blog/2020/01/15/springboot-step-by-step - 3/">





  <title>【四】SpringBoot创建项目——通用功能开发 | Aiolos</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Aiolos</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">记录我的成长点滴</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/blog/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://aiolos123.gitee.io/blog/blog/2020/01/15/springboot-step-by-step - 3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="aiolos">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Aiolos">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">【四】SpringBoot创建项目——通用功能开发</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-01-15T12:59:13+08:00">
                2020-01-15
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/SpringBoot/" itemprop="url" rel="index">
                    <span itemprop="name">SpringBoot</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>本文以商品管理功能开发为例，讲解使用SpringBoot开发通用功能的过程</p>
<a id="more"></a>

<h3 id="商品实体类及数据库表创建"><a href="#商品实体类及数据库表创建" class="headerlink" title="商品实体类及数据库表创建"></a>商品实体类及数据库表创建</h3><figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 商品实体类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@TableName</span>(<span class="string">"shop_items"</span>)</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> Items <span class="keyword">implements</span> Serializable &#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> final long serialVersionUID = <span class="number">-4761994755475901178</span>L;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@TableId</span>(<span class="keyword">type</span> = IdType.AUTO)</span><br><span class="line">    <span class="keyword">private</span> Long id; <span class="comment">//自增主键</span></span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">String</span> pic;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">String</span> name;</span><br><span class="line">    <span class="keyword">private</span> Double price;</span><br><span class="line">    <span class="keyword">private</span> ItemStatus status;<span class="comment">//状态</span></span><br><span class="line">    <span class="meta">@DateTimeFormat</span>(pattern = <span class="string">"yyyy-MM-dd"</span>)</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">Date</span> createDate; <span class="comment">//生产时间。驼峰命名法，默认对应到数据库表中的create_date字段</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 商品状态枚举：上架、下架</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Getter</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> ItemStatus&#123;</span><br><span class="line">    UP(<span class="number">0</span>,<span class="string">"上架"</span>),</span><br><span class="line">    DOWN(<span class="number">1</span>,<span class="string">"下架"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//使用@EnumValue标记数据库存的值是code</span></span><br><span class="line">    <span class="meta">@EnumValue</span></span><br><span class="line">    <span class="keyword">private</span> int code;</span><br><span class="line">    <span class="keyword">private</span> <span class="built_in">String</span> desc;</span><br><span class="line"></span><br><span class="line">    ItemStatus(int code, <span class="built_in">String</span> desc)&#123;</span><br><span class="line">        <span class="keyword">this</span>.code = code;</span><br><span class="line">        <span class="keyword">this</span>.desc = desc;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"># 并在yml中配置enums包扫描路径</span><br><span class="line">mybatis-plus:</span><br><span class="line">  <span class="keyword">type</span>-enums-package: com.aiolos.springbootbasic.enums</span><br></pre></td></tr></table></figure>

<p>数据库表如下图：<br><img src="/blog/images/20200114100606562.jpg" alt="数据库表"></p>
<h3 id="Mapper接口"><a href="#Mapper接口" class="headerlink" title="Mapper接口"></a>Mapper接口</h3><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">public<span class="built_in"> interface </span>ItemsMapper extends BaseMapper&lt;Items&gt; &#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Service接口与实现类"><a href="#Service接口与实现类" class="headerlink" title="Service接口与实现类"></a>Service接口与实现类</h3><figure class="highlight scala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public interface <span class="type">IItemsService</span> <span class="keyword">extends</span> <span class="type">IService</span>&lt;<span class="type">Items</span>&gt; &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">ItemServiceImpl</span> <span class="keyword">extends</span> <span class="title">ServiceImpl&lt;ItemsMapper</span>, <span class="title">Items&gt;</span> <span class="title">implements</span> <span class="title">IItemsService</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>上述3步是使用MP编写每一个CRUD功能的通用步骤，无需自己编写SQL</strong></p>
<blockquote>
<p>MP中的AR模式： 很强大，但不完全符合传统的Java编写风格。因为AR模式是直接通过实体类对象本身来进行CRUD操作，而无需使用Mapper、Service这些接口和实现类</p>
</blockquote>
<p><img src="/blog/images/20200114104128581.jpg" alt="MP中的AR模式"></p>
<h3 id="实体类中一对多功能的三种实现方式-关联查询的实现"><a href="#实体类中一对多功能的三种实现方式-关联查询的实现" class="headerlink" title="实体类中一对多功能的三种实现方式(关联查询的实现)"></a>实体类中一对多功能的三种实现方式(关联查询的实现)</h3><blockquote>
<p>通过字典表(<strong>见《9. 字典表数据映射》一节</strong>)；通过自定义关联查询SQL(<strong>不推荐使用</strong>)；通过在Service中封装。 下面为第三种方式的实现步骤：</p>
</blockquote>
<blockquote>
<p>a. 创建shop_category表，shop_items表中增加category_id字段，关联shop_category表的id字段。如下图：</p>
</blockquote>
<p><img src="/blog/images/20200114113759017.jpg" alt="创建shop_category表"></p>
<blockquote>
<p>b. 创建shop_category表对应的实体类、Mapper、Service，并在shop_items表的实体类Items中添加categoryId字段，然后<strong>在Service中通过这个字段查询其他实体类完成封装</strong></p>
</blockquote>
<p><img src="/blog/images/20200114164405962.jpg" alt="在Service中通过这个字段查询其他实体类完成封装"></p>
<p>注意：<br><strong>1. 如果是使用MP进行多表查询，需要自己在xml中编写sql</strong><br><strong>2. 实际开发中，不推荐在实体类中添加其他对象来表示对象之间的1:N,N:1,N:M的关系，因为这样做容易使实体类关系混乱；推荐使用在Service中通过字段查询其他实体类完成封装的方式</strong>，即如下图方式</p>
<p><img src="/blog/images/20200114164405962.jpg" alt="在Service中通过这个字段查询其他实体类完成封装"></p>
<h3 id="Controller返回商品数据"><a href="#Controller返回商品数据" class="headerlink" title="Controller返回商品数据"></a>Controller返回商品数据</h3><figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping(<span class="meta-string">"/items"</span>)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ItemController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ICategorySerive categorySerive;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> IItemsService itemsService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping(<span class="meta-string">"list"</span>)</span></span><br><span class="line">    <span class="keyword">public</span> String list(Model model)&#123;</span><br><span class="line">        <span class="comment">//分页查询商品</span></span><br><span class="line">        IPage&lt;Items&gt; page = itemsService.page(new Page&lt;&gt;(<span class="number">1</span>,<span class="number">10</span>));</span><br><span class="line">        model.addAttribute(<span class="string">"page"</span>,page);</span><br><span class="line">        <span class="comment">//同步方式获取所有商品类别</span></span><br><span class="line">        model.addAttribute(<span class="string">"categorylist"</span>,categorySerive.list());</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"items"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="前端页面动态展示商品数据"><a href="#前端页面动态展示商品数据" class="headerlink" title="前端页面动态展示商品数据"></a>前端页面动态展示商品数据</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">th</span>:each 迭代循环</span><br><span class="line"><span class="selector-tag">th</span>:<span class="keyword">if</span>  相当于<span class="keyword">if</span> </span><br><span class="line"><span class="selector-tag">th</span>:unless 相当于<span class="keyword">else</span> </span><br><span class="line"><span class="selector-tag">th</span>:switch 相当于switch</span><br></pre></td></tr></table></figure>

<p>下拉列表的数据展示如下：<br><img src="/blog/images/20200115055300547.jpg" alt="下拉列表的数据展示"></p>
<p>商品列表的数据展示如下：<br><img src="/blog/images/20200115063316018.jpg" alt="商品列表的数据展示"></p>
<blockquote>
<p>注意：上图中，<strong>通过字典表来显示商品类型名称,是最简单的方法。字典表包括：主键、类型label名称、类型key、类型value</strong>，详细步骤请见<strong>《9. 字典表数据映射》一节</strong></p>
</blockquote>
<h3 id="逻辑删除功能实现"><a href="#逻辑删除功能实现" class="headerlink" title="逻辑删除功能实现"></a>逻辑删除功能实现</h3><blockquote>
<p>MP自带逻辑删除功能</p>
</blockquote>
<ol>
<li><p>添加配置</p>
<figure class="highlight dsconfig"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">mybatis-plus:</span></span><br><span class="line">  <span class="string">global-config:</span></span><br><span class="line">    <span class="string">db-config:</span></span><br><span class="line">      <span class="string">logic-delete-</span><span class="string">field:</span> <span class="string">status </span> <span class="comment">#全局逻辑删除字段值 3.3.0开始支持，详情看下面。</span></span><br><span class="line">      <span class="string">logic-delete-</span><span class="string">value:</span> -1 <span class="comment"># 逻辑已删除值(默认为 1)</span></span><br><span class="line">      <span class="string">logic-not-</span><span class="built_in">delete-value:</span> 0 <span class="comment"># 逻辑未删除值(默认为 0)</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>实体类字段上加上@TableLogic注解(如果配置了全局逻辑删除，则可以不配置该注解)</p>
<figure class="highlight less"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable">@Data</span></span><br><span class="line"><span class="variable">@TableName</span>(<span class="string">"shop_items"</span>)</span><br><span class="line">public class Items implements Serializable &#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//其他字段同之前，未作变动</span></span><br><span class="line">    <span class="variable">@TableLogic</span></span><br><span class="line">    private Integer status;<span class="comment">//状态</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>全局逻辑删除: 3.3.0开始支持</p>
<blockquote>
<p>如果公司代码比较规范，比如统一了全局都是status为逻辑删除字段。<br>使用此配置则不需要在实体类上添加 @TableLogic。<br>但如果实体类上有 @TableLogic 则以实体上的为准，忽略全局。 即先查找注解再查找全局，都没有则此表没有逻辑删除。</p>
</blockquote>
</li>
<li><p>测试逻辑删除效果<br><img src="/blog/images/20200115065051054.jpg" alt="测试逻辑删除效果"><br><img src="/blog/images/20200115065429105.jpg" alt="测试逻辑删除效果"></p>
</li>
<li><p>前端页面实现逻辑删除效果(@DeleteMapping请求)<br><img src="/blog/images/20200117152628727.jpg" alt="前端页面实现逻辑删除效果"></p>
</li>
</ol>
<p><strong>注意：在thymeleaf模板中的js函数中，动态传递参数需要使用[[${item.id}]]这样的表达式，示例如上图th:onclick=”member_del(this,[[${item.id}]])”或下图th:onclick=”onSaleOrOffBtn(this,[[${item.id}]])”</strong></p>
<h3 id="上架下架功能实现"><a href="#上架下架功能实现" class="headerlink" title="上架下架功能实现"></a>上架下架功能实现</h3><ol>
<li><p>为上架下架开关添加事件——layer中的规则(在开关外层包裹div，为其添加事件；也可以通过监听事件实现该功能)<br><img src="/blog/images/20200117123751924.jpg" alt="为上架下架开关添加事件"></p>
</li>
<li><p>通过在checkbox中添加如下代码，实现初始化列表时即设置正确的上下架状态：</p>
<figure class="highlight vala"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">th:checked=<span class="string">"$&#123;item.status == 1?true:false&#125;"</span></span><br><span class="line"></span><br><span class="line"><span class="meta"># 其实现原理为：</span></span><br><span class="line"><span class="meta"># 可通过th:checked设置复选框的值； </span></span><br><span class="line"><span class="meta"># th:checked="true"表示选中，th:checked="false"表示未选中；</span></span><br><span class="line"><span class="meta"># 当th:checked=false时，thymeleaf解析时就不会设置th:checked属性，即显示上图的效果</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>上下架的后端实现逻辑<br><img src="/blog/images/20200117151137494.jpg" alt="上下架的后端实现逻辑"></p>
</li>
<li><p>解决快速连续点击上下架开关的问题——JS延时触发事件</p>
</li>
</ol>
<h3 id="字典表数据映射"><a href="#字典表数据映射" class="headerlink" title="字典表数据映射"></a>字典表数据映射</h3><blockquote>
<p>字典表的优点： 数据库实体类之间无需编写关联SQL；各表只与字典表关联；</p>
</blockquote>
<ol>
<li><p>因为要使用字典表做商品类型，所以Category表、实体类、相关的Service、Mapper都没有作用，可以删除！</p>
</li>
<li><p>创建字典表shop_dict(格式一般都固定)<br><img src="/blog/images/20200115125621881.jpg" alt="创建字典表"></p>
</li>
<li><p>创建字典表其对应的实体类、相关的Service、Mapper接口和实现类</p>
</li>
<li><p>在WebConfig中编写自定义字典表输出格式的注册器和对应的工具类(通用代码)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WebConfig</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 注册自定义方言，为thymeleaf模板输出自定义方法结果提供支持</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="meta">@ConditionalOnMissingBean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> CustomDialect <span class="title">customDialect</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> CustomDialect(<span class="string">"Dict Dialect"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 自定义方言类</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@ConditionalOnMissingBean</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomDialect</span> <span class="keyword">extends</span> <span class="title">AbstractDialect</span> <span class="keyword">implements</span> <span class="title">IExpressionObjectDialect</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">CustomDialect</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(name);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> IExpressionObjectFactory <span class="title">getExpressionObjectFactory</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> IExpressionObjectFactory() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Set&lt;String&gt; <span class="title">getAllExpressionObjectNames</span><span class="params">()</span> </span>&#123;</span><br><span class="line">                <span class="comment">//以单例模式创建dic的对象</span></span><br><span class="line">                <span class="keyword">return</span> Collections.singleton(<span class="string">"dic"</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Object <span class="title">buildObject</span><span class="params">(IExpressionContext iExpressionContext, String s)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> DictUtils();</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isCacheable</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 字典工具类</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DictUtils</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> IDictService service;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> IDictService sysDictService;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostConstruct</span> <span class="comment">//完成对Service的注入</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span>&#123;</span><br><span class="line">        sysDictService = service;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取字段值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> type</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> code</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getDictValue</span><span class="params">(String type, Integer code)</span></span>&#123;</span><br><span class="line">        <span class="comment">//缓存中数据，则db</span></span><br><span class="line">        Dict dict = sysDictService.getOne(<span class="keyword">new</span> QueryWrapper&lt;Dict&gt;()</span><br><span class="line">                .eq(<span class="string">"type"</span>, type).eq(<span class="string">"code"</span>, code));</span><br><span class="line">        <span class="keyword">return</span> dict.getLabel();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取字典value值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> type</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> label</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getCode</span><span class="params">(String type, String label)</span></span>&#123;</span><br><span class="line">        <span class="comment">//缓存中数据，则db</span></span><br><span class="line">        Dict dict = sysDictService.getOne(<span class="keyword">new</span> QueryWrapper&lt;Dict&gt;()</span><br><span class="line">                .eq(<span class="string">"type"</span>, type).eq(<span class="string">"label"</span>, label));</span><br><span class="line">        <span class="keyword">return</span> dict.getCode();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取某种类型的字典列表</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> type</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Dict&gt; <span class="title">getDictList</span><span class="params">(String type)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> sysDictService.list(<span class="keyword">new</span> QueryWrapper&lt;Dict&gt;()</span><br><span class="line">                .eq(<span class="string">"type"</span>, type));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>前端页面自定义thymeleaf表达式，实现通过字典表来展示商品类型数据：<br><img src="/blog/images/20200117161400094.jpg" alt="实现通过字典表来展示商品类型数据"></p>
</li>
</ol>
<h3 id="新增功能"><a href="#新增功能" class="headerlink" title="新增功能"></a>新增功能</h3><ol>
<li>编写页面跳转的controller</li>
<li>编写前端页面通过ajax异步提交表单<blockquote>
<p>摘录自： <a href="https://blog.csdn.net/yw_1207/article/details/79626145" target="_blank" rel="noopener">https://blog.csdn.net/yw_1207/article/details/79626145</a></p>
</blockquote>
</li>
</ol>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"># 修改成AJAX提交表单的三个步骤</span><br><span class="line"># <span class="number">1.</span> 将form元素的属性action和method去掉，添加id=”myForm”,form元素就变为&lt;form id=<span class="string">"myForm"</span>&gt;</span><br><span class="line"># <span class="number">2.</span> 将提交按钮的button的<span class="keyword">type</span>=”submit”改为<span class="keyword">type</span>=”button”，增加 id，变为&lt;input <span class="keyword">type</span>=<span class="string">"button"</span> id=<span class="string">"submitBtn"</span>&gt;</span><br><span class="line"># <span class="number">3.</span> 在js中为为submitBtn按钮添加点击事件，在其中处理表单提交</span><br><span class="line">$(<span class="string">"#submitBtn"</span>).click(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//处理表单提交</span></span><br><span class="line">    $.ajax(&#123;</span><br><span class="line">        <span class="keyword">type</span>: <span class="string">"POST"</span>,   <span class="comment">//提交的方法</span></span><br><span class="line">        url:<span class="string">"add"</span>, <span class="comment">//提交的地址</span></span><br><span class="line">        data:$(<span class="string">'#myForm'</span>).serialize(),<span class="comment">// 序列化表单值</span></span><br><span class="line">        error: <span class="function"><span class="keyword">function</span>(<span class="params">request</span>) </span>&#123;  </span><br><span class="line">          <span class="comment">//提交表单失败                 </span></span><br><span class="line">          alert(<span class="string">"Connection error"</span>);</span><br><span class="line">        &#125;,</span><br><span class="line">        success: <span class="function"><span class="keyword">function</span>(<span class="params">data</span>) </span>&#123;  </span><br><span class="line">          <span class="comment">//提交表单成功                 </span></span><br><span class="line">          alert(data);  <span class="comment">//就将返回的数据显示出来</span></span><br><span class="line">          <span class="built_in">window</span>.location.href=<span class="string">"跳转页面"</span> </span><br><span class="line">       &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>编写处理添加的controller<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PostMapping(<span class="meta-string">"add"</span>)</span></span><br><span class="line"><span class="meta">@ResponseBody</span></span><br><span class="line"><span class="keyword">public</span> String add(Items items)&#123;</span><br><span class="line">    <span class="comment">//form表单提交的数据自动封装为Items对象</span></span><br><span class="line">    boolean isSuccess = itemsService.save(items);</span><br><span class="line">    <span class="keyword">if</span>(isSuccess)&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"1"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"0"</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h3 id="抽取公共页面"><a href="#抽取公共页面" class="headerlink" title="抽取公共页面"></a>抽取公共页面</h3><ol>
<li><p>页面使用绝对路径<br><img src="/blog/images/20200118064344937.jpg" alt="页面使用绝对路径"></p>
</li>
<li><p>抽取公共页面</p>
<figure class="highlight dust"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"># 首先编写一个公共页面common.html,代码如下：</span></span><br><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span> <span class="attr">class</span>=<span class="string">"x-admin-sm"</span> <span class="attr">xmlns:th</span>=<span class="string">"http://www.thymeleaf.org"</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"header"</span> <span class="attr">th:fragment</span>=<span class="string">"header"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"renderer"</span> <span class="attr">content</span>=<span class="string">"webkit|ie-comp|ie-stand"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"X-UA-Compatible"</span> <span class="attr">content</span>=<span class="string">"IE=edge,chrome=1"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"</span> /&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"Cache-Control"</span> <span class="attr">content</span>=<span class="string">"no-siteapp"</span> /&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">href</span>=<span class="string">"../static/xadmin/css/font.css"</span> <span class="attr">th:href</span>=<span class="string">"@</span></span></span><span class="template-tag">&#123;/<span class="name">xadmin</span>/css/font.css&#125;</span><span class="xml"><span class="tag"><span class="string">"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">href</span>=<span class="string">"../static/xadmin/css/login.css"</span> <span class="attr">th:href</span>=<span class="string">"@</span></span></span><span class="template-tag">&#123;/<span class="name">xadmin</span>/css/login.css&#125;</span><span class="xml"><span class="tag"><span class="string">"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">"stylesheet"</span> <span class="attr">href</span>=<span class="string">"../static/xadmin/css/xadmin.css"</span> <span class="attr">th:href</span>=<span class="string">"@</span></span></span><span class="template-tag">&#123;/<span class="name">xadmin</span>/css/xadmin.css&#125;</span><span class="xml"><span class="tag"><span class="string">"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span> <span class="attr">src</span>=<span class="string">"https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"</span> <span class="attr">th:src</span>=<span class="string">"@</span></span></span><span class="template-tag">&#123;/<span class="name">js</span>/jquery.min.js&#125;</span><span class="xml"><span class="tag"><span class="string">"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"../static/xadmin/lib/layui/layui.js"</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span> <span class="attr">th:src</span>=<span class="string">"@</span></span></span><span class="template-tag">&#123;/<span class="name">xadmin</span>/lib/layui/layui.js&#125;</span><span class="xml"><span class="tag"><span class="string">"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">th:inline</span>=<span class="string">"javascript"</span>&gt;</span></span></span><br><span class="line"><span class="xml">        var base = [[$</span><span class="template-tag">&#123;#<span class="name">request.getContextPath</span>()&#125;</span><span class="xml">]]</span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="comment">&lt;!--[if lt IE 9]&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/html5shiv/r29/html5.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">![endif]--</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>然后在其他页面引用这个公共页面即可<br><img src="/blog/images/20200118064948102.jpg" alt="在其他页面引用这个公共页面"></p>
<h3 id="多环境切换"><a href="#多环境切换" class="headerlink" title="多环境切换"></a>多环境切换</h3><p>有多种方式，但推荐使用在一个yml文件编写三段配置的方式，如下图：<br><img src="/blog/images/20200118065541708.jpg" alt="推荐使用在一个yml文件编写三段配置的方式"></p>
<p>本人的配置如下：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 切换环境的配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  profiles:</span></span><br><span class="line"><span class="attr">    active:</span> <span class="string">dev</span></span><br><span class="line"></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># 生产环境配置</span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">9999</span></span><br><span class="line"><span class="comment"># 生产环境配置静态资源访问路径</span></span><br><span class="line"><span class="attr">web:</span></span><br><span class="line"><span class="attr">  upload-path:</span> <span class="string">/opt/img/</span></span><br><span class="line"></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="comment"># 配置数据库连接</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">pgsql的驱动</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:postgresql://192.168.32.10:5432/springboot-mybatis</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">postgres</span></span><br><span class="line"><span class="attr">    password:</span> </span><br><span class="line">  <span class="comment"># 配置静态资源访问路径</span></span><br><span class="line"><span class="attr">  mvc:</span></span><br><span class="line"><span class="attr">    static-path-pattern:</span> <span class="string">/**</span></span><br><span class="line"><span class="attr">  resources:</span></span><br><span class="line"><span class="attr">    static-locations:</span> <span class="attr">classpath:/META-INF/resources,</span> <span class="attr">classpath:/resources/,classpath:/static/,classpath:/public/,file:$&#123;web.upload-path&#125;</span></span><br><span class="line"><span class="attr">  profiles:</span> <span class="string">prod</span></span><br><span class="line"></span><br><span class="line"><span class="attr">logging:</span></span><br><span class="line"><span class="attr">    level:</span></span><br><span class="line"><span class="attr">      com:</span></span><br><span class="line"><span class="attr">        aiolos:</span> <span class="string">DEBUG</span></span><br><span class="line"></span><br><span class="line"><span class="meta">---</span></span><br><span class="line"><span class="comment"># 开发环境配置</span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8080</span></span><br><span class="line"><span class="comment"># 配置静态资源访问路径</span></span><br><span class="line"><span class="attr">web:</span></span><br><span class="line"><span class="attr">  upload-path:</span> <span class="attr">D:/dev/img/</span></span><br><span class="line"></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="comment"># 配置数据库连接</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">com.p6spy.engine.spy.P6SpyDriver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:p6spy:postgresql://localhost:5432/springboot-mybatis</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">postgres</span></span><br><span class="line"><span class="attr">    password:</span> </span><br><span class="line">  <span class="comment"># 配置静态资源访问路径</span></span><br><span class="line"><span class="attr">  mvc:</span></span><br><span class="line"><span class="attr">    static-path-pattern:</span> <span class="string">/**</span></span><br><span class="line"><span class="attr">  resources:</span></span><br><span class="line"><span class="attr">    static-locations:</span> <span class="attr">classpath:/META-INF/resources,</span> <span class="attr">classpath:/resources/,classpath:/static/,classpath:/public/,file:$&#123;web.upload-path&#125;</span></span><br><span class="line"><span class="attr">  profiles:</span> <span class="string">dev</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 公用的MP配置</span></span><br><span class="line"><span class="attr">mybatis-plus:</span></span><br><span class="line">  <span class="comment"># 枚举包扫描路径</span></span><br><span class="line"><span class="attr">  type-enums-package:</span> <span class="string">com.aiolos.springbootbasic.enums</span></span><br><span class="line">  <span class="comment"># 配置逻辑删除</span></span><br><span class="line"><span class="attr">  global-config:</span></span><br><span class="line"><span class="attr">    db-config:</span></span><br><span class="line">      <span class="comment"># logic-delete-field: isDel  #全局逻辑删除字段值 3.3.0开始支持，详情看下面。</span></span><br><span class="line"><span class="attr">      logic-delete-value:</span> <span class="bullet">-1</span> <span class="comment"># 逻辑已删除值(默认为 1)</span></span><br><span class="line"><span class="attr">      logic-not-delete-value:</span> <span class="number">0</span> <span class="comment"># 逻辑未删除值(默认为 0)</span></span><br></pre></td></tr></table></figure>

<p>切换不同的配置的效果如下图：<br><img src="/blog/images/20200118070922102.jpg" alt="切换不同的配置的效果"></p>
<h3 id="课程总结"><a href="#课程总结" class="headerlink" title="课程总结"></a>课程总结</h3><p>本课程学习的如下内容：</p>
<ol>
<li>后端管理页面X-admin的使用</li>
<li>页面模板thymeleaf的使用</li>
<li>SpringBoot+MP创建项目过程</li>
<li>字典表的使用</li>
<li>使用MP开发的通用步骤：实体类、Mapper、Service接口和实现类</li>
<li>热部署的配置</li>
<li>代码地址： code/SpringBoot-basic</li>
<li>学习视频：《2019年最新孙老师 springboot教程》</li>
</ol>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/SpringBoot创建项目/" rel="tag"># SpringBoot创建项目</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2020/01/10/how-to-use-coalesce-in-pgsql/" rel="next" title="Pgsql中的常用函数用法详解">
                <i class="fa fa-chevron-left"></i> Pgsql中的常用函数用法详解
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/blog/2020/01/19/second-kill-system-step-by-step-1/" rel="prev" title="【一】聚焦Java性能优化 打造亿级流量秒杀系统——搭建电商秒杀项目">
                【一】聚焦Java性能优化 打造亿级流量秒杀系统——搭建电商秒杀项目 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">aiolos</p>
              <p class="site-description motion-element" itemprop="description">Java Spring Hadoop 机器学习</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/blog/archives/">
              
                  <span class="site-state-item-count">126</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/blog/categories/index.html">
                  <span class="site-state-item-count">16</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/blog/tags/index.html">
                  <span class="site-state-item-count">33</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#商品实体类及数据库表创建"><span class="nav-number">1.</span> <span class="nav-text">商品实体类及数据库表创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Mapper接口"><span class="nav-number">2.</span> <span class="nav-text">Mapper接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Service接口与实现类"><span class="nav-number">3.</span> <span class="nav-text">Service接口与实现类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实体类中一对多功能的三种实现方式-关联查询的实现"><span class="nav-number">4.</span> <span class="nav-text">实体类中一对多功能的三种实现方式(关联查询的实现)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Controller返回商品数据"><span class="nav-number">5.</span> <span class="nav-text">Controller返回商品数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#前端页面动态展示商品数据"><span class="nav-number">6.</span> <span class="nav-text">前端页面动态展示商品数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#逻辑删除功能实现"><span class="nav-number">7.</span> <span class="nav-text">逻辑删除功能实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#上架下架功能实现"><span class="nav-number">8.</span> <span class="nav-text">上架下架功能实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#字典表数据映射"><span class="nav-number">9.</span> <span class="nav-text">字典表数据映射</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#新增功能"><span class="nav-number">10.</span> <span class="nav-text">新增功能</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#抽取公共页面"><span class="nav-number">11.</span> <span class="nav-text">抽取公共页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多环境切换"><span class="nav-number">12.</span> <span class="nav-text">多环境切换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#课程总结"><span class="nav-number">13.</span> <span class="nav-text">课程总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">aiolos</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/blog/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/blog/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/blog/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/blog/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/blog/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/blog/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/blog/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/blog/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
